package com.chianghao.rock.system.service.system_param;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.chianghao.core.exception.HaoException;
import com.chianghao.core.frame.service.BaseService;
import com.chianghao.rock.system.dao.system_param.SystemParamMapper;
import com.chianghao.rock.system.entity.SystemParam;

@Service
public class SystemParamService extends BaseService<SystemParamMapper, SystemParam> {


	public SystemParam queryByCode(String Code) {
		SystemParam where  = new SystemParam();
		where.setCode(Code);
		return this.queryBean(where);
	}

	
	private boolean isExist(String newCode,String code) {
		int count = this.mapper.queryCountAsNewCodeWhereCode(newCode,code);
		return count>0?true:false;
	}
	
	
	
	
	public void update(SystemParam bean,String code) {
		//判断code是否存在
		if(StringUtils.isNotEmpty(bean.getCode())) {
			if(isExist(bean.getCode(),code)) {
				throw new HaoException("error:code,not.repeatable");
			}
		}
		this.updateByCode(bean, code);
	}
	

	public void doAdd(SystemParam systemParam) {
		SystemParam sp = queryByCode(systemParam.getCode());
		if(sp!=null) {
			throw new HaoException("error:code,not.repeatable");
		}
		this.insert(systemParam);
	}

	public void doDelete(String code) {
		//校验是否可以删除
		SystemParam sp = queryByCode(code); 
		if(sp==null) {
			return;
		}
		if(!sp.getDeletable()) {
			throw new HaoException("error:code,unable.delete");
		}
		SystemParam where  = new SystemParam();
		where.setCode(code);
		
		this.mapper.deleteByEntity(where);
	}




}